import axios from 'axios'
import { message } from "ant-design-vue";
// console.log(process.env.VUE_APP_BASE_API);

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  timeout: 50000, // request timeout
  headers: {
    'session': sessionStorage.getItem('session')
  }
})
// console.log(sessionStorage.getItem('session'));

service.interceptors.request.use(
  config => {
    // if (store.getters.token) {
    //   config.headers.Authorization = getToken()
    //   config.headers.Accept = 'application/json'
    //   config.headers.VC = vc(config.data)
    //   config.headers.marketId = store.state.app.marketId
    // }
    const token = sessionStorage.getItem('session')
    token ? config.headers.Authorization = token : null
    return config
  },
  error => {
    //   console.log(error)
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    const res = response.data
    if (res.code != 200) {
      if (res.code == 403) {
        top.window.location = 'http://login.zhinengjianshe.com/cas/login?service=http://192.168.1.42:8080/#/workbench/index'
      }
      message.error(res.message)
      return Promise.reject(new Error(res.message || 'Error'))
    } else {
      return response
    }
  }, error => {
    let mes = ''
    if (error.response.data.error) {
      mes = error.response.data.error
    }
    else mes = error.response.data.message
    console.log(error.response);

    return Promise.reject(error)
  })

export default service